package com.gopay.bis.manager.bank.Wap;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 兴业wap支付B2C对象封装
 * 
 * @ClassName: CibWapB2cManager
 * @author w_jiqing
 * @date 2017年3月16日 下午3:15:28
 */
public class CibWapB2cManager implements WapPayManagerRemoteService {

    @Resource(name = "cibB2cBankServiceClient")
    private BankService cibB2cBankServiceClient;

    @Resource(name = "cibConfig")
    private Map<String, String> cibConfig;

    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        CibMessage messageB2C = (CibMessage) msg;
        messageB2C = buildSignMessage(messageB2C);
        BankLogUtils.printB2CManagerSignSendLog(messageB2C);
        String signature = (String) cibB2cBankServiceClient.sign(messageB2C);
        messageB2C.setMac(signature);
        BankLogUtils.printB2CManagerSignReturnLog(messageB2C);
        return messageB2C;
    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        CibMessage messageB2C = (CibMessage) msg;
        messageB2C = buildVerifyMessag(messageB2C);
        BankLogUtils.printB2CManagerVerifySendLog(messageB2C);
        boolean isVerify = (Boolean) cibB2cBankServiceClient.verify(messageB2C);
        BankLogUtils.printB2CManagerVerifyReturnLog(messageB2C, isVerify);
        return isVerify;
    }

    @Override
    public Object query(Object obj) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object dayend(BaseMessage base) throws GopayException, Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 组装WAP签名对象
     * @param messageB2C
     * @return
     */
    private CibMessage buildSignMessage(CibMessage messageB2C) {
        messageB2C.setBankUrl(messageB2C.getPayUrl());
        messageB2C.setService(cibConfig.get("CIB.WAP.SERVICE"));
        messageB2C.setVer(cibConfig.get("CIB.B2C.VER"));
        messageB2C.setOrdDate(messageB2C.getOrderDate().substring(0, 8));
        messageB2C.setCur(cibConfig.get("CIB.B2C.CUR"));
        messageB2C.setOrdAmt(messageB2C.getAmount().toString());
        String mrchNo = messageB2C.getMerID();
        // 拼装签名明文
        String plainMessage = "service" + messageB2C.getService() + "ver" + messageB2C.getVer() + "mrch_no" + mrchNo
                + "ord_no" + messageB2C.getOrderId() + "ord_date" + messageB2C.getOrdDate() + "cur"
                + messageB2C.getCur() + "ord_amt" + messageB2C.getOrdAmt() + messageB2C.getPrivateKeyPath();
        messageB2C.setPlainMessage(plainMessage.trim());
        messageB2C.setMrchNo(mrchNo);
        return messageB2C;
    }
    
    /**
     * 组装B2C验签对象 
     * @param verifyMessage
     * @return
     */
    private CibMessage buildVerifyMessag(CibMessage verifyMessage) {
        String plainMessage = "bank" + verifyMessage.getBankCode() + "mrch_no" + verifyMessage.getMrchNo() + "ord_no"
                + verifyMessage.getOrdNo() + "cur" + verifyMessage.getCur() + "ord_amt" + verifyMessage.getOrdAmt()
                + "pay_amt" + verifyMessage.getPayAmt() + "sno" + verifyMessage.getSno()
                + verifyMessage.getPrivateKeyPath();
        verifyMessage.setPlainMessage(plainMessage);
        return verifyMessage;
    }

}
